package com.ruoyi.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.provider.domain.AhctDefineDamage;
import com.ruoyi.provider.domain.AhctDefineDamageProject;
import com.ruoyi.provider.mapper.AhctDefineDamageMapper;
import com.ruoyi.provider.mapper.AhctDefineDamageProjectMapper;
import com.ruoyi.provider.service.AhctDefineDamageProjectService;
import com.ruoyi.provider.service.AhctImageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Wang RuLong
* @description 针对表【ahct_define_damage_project(定损项目列表)】的数据库操作Service实现
* @createDate 2024-05-17 09:51:10
*/
@Service
public class AhctDefineDamageProjectServiceImpl extends ServiceImpl<AhctDefineDamageProjectMapper, AhctDefineDamageProject>
    implements AhctDefineDamageProjectService {


    @Resource
    private AhctDefineDamageProjectMapper ahctDefineDamageProjectMapper;

    @Resource
    private AhctImageService ahctImageService;

    @Resource
    private AhctDefineDamageMapper ahctDefineDamageMapper;



    //获取定损尾部列表
    @Override
    public List<AhctDefineDamageProject> getDamageProperty(String case_number) {

        List<AhctDefineDamageProject> damageList;
        damageList = ahctDefineDamageProjectMapper.getDamageProperty(case_number);
        return damageList;
    }


    //新增定损项目列表
    @Transactional
    @Override
    public void insertDamage(AhctDefineDamageProject ahctDefineDamageProject,List<AhctDefineDamageProject> ItemQuantity) {

//        BigDecimal sum = new BigDecimal(0);//用于统计总金额
//        BigDecimal bigDecimalPrice = new BigDecimal(0);//用于统计当前案件金额
//        AhctDefineDamage defineDamage = new AhctDefineDamage();
        for (AhctDefineDamageProject project : ItemQuantity){
            ahctDefineDamageProject.setDamage_project_name(project.getDamage_project_name());
            ahctDefineDamageProject.setDamage_project_number(project.getDamage_project_number());
            ahctDefineDamageProject.setDamage_project_price(project.getDamage_project_price());
            ahctDefineDamageProject.setUnit(project.getUnit());
            ahctDefineDamageProject.setRemarks(project.getRemarks());
            ahctDefineDamageProjectMapper.insertDamage(ahctDefineDamageProject);

            //可以这样写遍历
//            for (int j = 0; j < listSize; j++) {
//                BusinessFeeReturn order = list.get(j);
//
//                List<Object> rowData = new ArrayList<>();
//                rowData.add(DateUtils.DateToString(order.getUpdateTime(), Constants.FORMAT));//交易时间

            //计算我司综合报价
//            BigDecimal bigDecimal = BigDecimal.valueOf(ahctDefineDamageProject.getDamage_project_number());
//            bigDecimalPrice = ahctDefineDamageProject.getDamage_project_price();
//            defineDamage.setCase_number(ahctDefineDamageProject.getCase_number());
//            sum = sum.add(bigDecimalPrice.multiply(bigDecimal));
        }
        //获取删除当前信息的案件号
//        defineDamage.setCase_number(ahctDefineDamageProject.getCase_number());
//        //给计算后的金额插入到定损报批表中
//        defineDamage.setOur_price(sum);
//        ahctDefineDamageMapper.updateDamagePrice(defineDamage);


        //判断ItemQuantity如果没有值,则删除定损报批表中税金、施工费用、总价
//        if (ItemQuantity.isEmpty()){
//            deleteLossPrice(ahctDefineDamageProject);
//        }else {
            //定损详情提交时保存税金、施工费用、总价到定损报批表
            updateLossPrice(ahctDefineDamageProject);
//        }
    }


    //定损详情提交时保存税金、施工费用、总价到定损报批表
    public void updateLossPrice(AhctDefineDamageProject ahctDefineDamageProject){
        AhctDefineDamage ahctDefineDamage = new AhctDefineDamage();
        ahctDefineDamage.setCase_number(ahctDefineDamageProject.getCase_number());
        ahctDefineDamage.setTaxes(ahctDefineDamageProject.getTaxes());
        ahctDefineDamage.setConstruction_amount(ahctDefineDamageProject.getConstruction_amount());
        ahctDefineDamage.setTotal_amount(ahctDefineDamageProject.getTotal_amount());

        ahctDefineDamageMapper.updatePrice(ahctDefineDamage);
    }

    //删除定损报批表中税金、施工费用、总价
//    public void deleteLossPrice(AhctDefineDamageProject ahctDefineDamageProject){
//        AhctDefineDamage ahctDefineDamage = new AhctDefineDamage();
//        ahctDefineDamage.setCase_number(ahctDefineDamageProject.getCase_number());
//
//        ahctDefineDamageMapper.deletePrice(ahctDefineDamage);
//    }


    //删除定损项目详情 + 照片
    @Transactional
    @Override
    public void deleteList(AhctDefineDamageProject ahctDefineDamageProject) {

        //删除项目详情
        ahctDefineDamageProjectMapper.deleteList(ahctDefineDamageProject.getDefine_damage_id());
        //删除图片
        ahctImageService.deleteImg(ahctDefineDamageProject.getCase_number());
    }


    @Override
    public Integer selectProjectId(String damage_project_name, Integer define_damage_id) {

        Integer list;
        list = ahctDefineDamageProjectMapper.selectProjectId(damage_project_name,define_damage_id);
        return list;
    }


    @Override
    public AhctDefineDamage selectByCaseNumber(String case_number) {
        return ahctDefineDamageMapper.selectByCaseNumber(case_number);
    }


}




